package com.zyb.ext.arrayList;

import java.util.Arrays;

public class MyArrayList<E> implements MyList<E> {

    //初始容量
    private static final int DEFAULT_CAPACITY = 10;
    //定义属组
    private Object[] elementData;
    //实际存储数据的大小
    private int size;

    //初始化集合，使用默认的容量
    public MyArrayList(int initCapacity) {
        if (initCapacity > 0) {
            this.elementData = new Object[initCapacity];
        } else {
            throw new IllegalArgumentException("请指定大于0的初始容量 " + initCapacity);
        }
    }

    public MyArrayList() {
        this(DEFAULT_CAPACITY);
    }

    @Override
    public boolean add(E e) {
        //验证是否需要扩容操作
        checkCapacity(size + 1);
        elementData[size++] = e;
        return true;
    }

    //验证是否需要扩容操作
    private void checkCapacity(int minCapacity) {
        //实际存储的值大于等于和容量相等时，需要进行扩容
        if (size >= elementData.length) {
            int oldLength = elementData.length;
            //扩容的大小，默认为1.5倍,但如果oldLength为1时呢？
            int newLength = oldLength + (oldLength >> 1);
            //如果oldLength为1时，将容器扩容为2倍，即：1+1
            if (newLength < minCapacity) {
                newLength = minCapacity;
            }
            elementData = Arrays.copyOf(elementData, newLength);
        }
    }

    @Override
    public E get(int index) {
        if (index >= size) {
            throw new IndexOutOfBoundsException("指定的索引超出范围了...");
        }
        return (E) elementData[index];
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean add(int index, E e) {
        if (index < 0 || index > this.size) {
            throw new IndexOutOfBoundsException("add >>> 请指定有效的索引...");
        }
        //验证是否需要扩容操作
        checkCapacity(size + 1);
        //把原来的数据向后复制一位
        System.arraycopy(elementData, index, elementData, index + 1, size - index);
        //在移位处填入新的值
        elementData[index] = e;
        size++;
        return true;
    }

    @Override
    public E remove(int index) {
        if (index < 0 || index > this.size) {
            throw new IndexOutOfBoundsException("remove >>> 请指定有效的索引...");
        }
        //获取此索引位置的值
        Object oldElement = elementData[index];
        //把原来的数据向前复制一位
        System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
        //末尾位置数据置空
        elementData[--size] = null;
        return (E) oldElement;
    }

    @Override
    public boolean remove(E e) {
        if (e == null) {
            throw new RuntimeException("remove 入参对象不能为空！");
        }
        for (int index = 0; index < size; index++) {
            Object elementDatum = elementData[index];
            //判断是否存在此对象
            if (elementDatum.equals(e)) {
                remove(index);
                return true;
            }
        }
        return false;
    }

    @Override
    public void clear() {
        for (int index = 0; index < size; index++) {
            elementData[index] = null;
        }
        size = 0;
    }
}














